package StudyCode.summary.myfile;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class NormalizePathEnsureStartWithFileSeparator {

	public static void main(String[] args) {
		System.out.println(normalizePath("E:/Copy", '/'));
	}

	public static String normalizePath(String path, char sep) {
		boolean needLeadingSlash = path.charAt(0) != sep;
		boolean needRemoveTrailingSlash = path.charAt(path.length() - 1) == sep;
		if (needLeadingSlash || needRemoveTrailingSlash) {
			if (needLeadingSlash && needRemoveTrailingSlash) {
				StringBuilder b = new StringBuilder();
				b.append(sep);
				b.append(path, 0, path.length() - 1);
				return b.toString();
			} else if (needLeadingSlash) {
				StringBuilder b = new StringBuilder();
				b.append(sep);
				b.append(path);
				return b.toString();
			} else {
				return path.substring(0, path.length() - 1);
			}
		}
		return path;
	}
}
